package dto

import "nexus/model"

type DTOTransfer struct{}

func NewDTOTransfer() *DTOTransfer {
	return &DTOTransfer{}
}

// user
// model -> dto
func (dto *DTOTransfer) UserToDTO(user *model.User, tenant *model.Tenant) *User {
	dtoTenant := &Tenant{}
	if tenant != nil {
		dtoTenant = dto.TenantToDTO(tenant)
	}
	return &User{
		ID:        user.ID,
		Username:  user.Username,
		Password:  user.Password,
		Nickname:  user.Nickname,
		AvatarID:  user.AvatarID,
		Email:     user.Email,
		Phone:     user.Phone,
		IsSuper:   user.IsSuper,
		Status:    user.Status,
		CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt: user.UpdatedAt.Format("2006-01-02 15:04:05"),
		Tenant:    dtoTenant,
	}
}

// 同租户User批量转换
func (dto *DTOTransfer) BatchUserToDTO(users []*model.User, tenants *model.Tenant) []*User {
	var dtoUsers []*User
	for _, user := range users {
		dtoUsers = append(dtoUsers, dto.UserToDTO(user, tenants))
	}
	return dtoUsers
}

// tenant
// model -> dto
func (dto *DTOTransfer) TenantToDTO(tenant *model.Tenant) *Tenant {
	deletedAt := ""
	if tenant.DeletedAt != nil {
		deletedAt = tenant.DeletedAt.Format("2006-01-02 15:04:05")
	}

	return &Tenant{
		ID:            tenant.ID,
		Name:          tenant.Name,
		ServerStartAt: tenant.ServerStartAt,
		ServerEndAt:   tenant.ServerEndAt,
		Status:        tenant.Status,
		CreatedAt:     tenant.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt:     tenant.UpdatedAt.Format("2006-01-02 15:04:05"),
		DeletedAt:     deletedAt,
	}
}

// 同租户批量转换
func (dto *DTOTransfer) BatchTenantToDTO(tenants []*model.Tenant) []*Tenant {
	var dtoTenants []*Tenant
	for _, tenant := range tenants {
		dtoTenants = append(dtoTenants, dto.TenantToDTO(tenant))
	}
	return dtoTenants
}

// file
// model -> dto
func (dto *DTOTransfer) FileToDTO(file *model.File) *File {
	deletedAt := ""
	if file.DeletedAt != nil {
		deletedAt = file.DeletedAt.Format("2006-01-02 15:04:05")
	}

	return &File{
		ID:           file.ID,
		OriginalName: file.OriginalName,
		DisplayName:  file.DisplayName,
		FileType:     file.FileType,
		Path:         file.Path,
		Status:       file.Status,
		CreatedAt:    file.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt:    file.UpdatedAt.Format("2006-01-02 15:04:05"),
		DeletedAt:    deletedAt,
	}

}
